local assets = {
	Asset("ANIM", "anim/musha_sword_long.zip"),
	Asset("ANIM", "anim/musha_sword_long_swap.zip")
}

local function Updata(inst, display)
	if inst.components.musha_equipment:IsBroken() then
		inst.components.weapon:SetDamage(1)
		return
	end
	if not inst.components.tool then
		inst:AddComponent("tool")
		inst.components.tool:SetAction(ACTIONS.DIG)
	end
	if inst.components.musha_equipment.currentlevel == 20 then
		inst:PushEvent("level20")
	end
	inst.components.weapon:SetDamage(34 + inst.components.musha_equipment.currentlevel * 2)
	if display then
		inst.components.talker:Say(
			string.format(
				STRINGS.MUSHA_WEAPON_SWORD_LONG .. " (LV%d)\n" .. STRINGS.MUSHA_WEAPON_DAMAGE .. " (%d)",
				inst.components.musha_equipment.currentlevel,
				34 + inst.components.musha_equipment.currentlevel * 2
			)
		)
	end
end

local function OnAttack(inst, attacker, target)
	if inst.components.musha_equipment:IsBroken() then
		local splash = SpawnPrefab("splash")
		local pos = Vector3(target.Transform:GetWorldPosition())
		splash.Transform:SetPosition(pos:Get())
		return
	end
	if
		target and not target:HasTag("structure") and not target:HasTag("stalkerminion") and not target:HasTag("smashable") and
			not target:HasTag("alignwall") and
			not target:HasTag("shadowminion") and
			target.components.health and
			not target.components.health:IsDead()
	 then
		if
			target.components.freezable and
				math.random() < inst.components.musha_equipment.currentlevel * (inst.EnergyOn and 1.2 or 0.7)
		 then
			local fx = SpawnPrefab("groundpoundring_fx")
			local pos = Vector3(target.Transform:GetWorldPosition())
			fx.Transform:SetScale(0.3, 0.3, 0.3)
			fx.Transform:SetPosition(pos:Get())
			local coldness = inst.components.musha_equipment.currentlevel * (inst.EnergyOn and 1.2 or 0.7) * 1.2
			target.components.freezable:AddColdness(coldness)
			target.components.freezable:SpawnShatterFX()
			target.components.health:DoDelta(-2)
			local prefab = "icespike_fx_" .. math.random(1, 4)
			local fx = SpawnPrefab(prefab)
			fx.Transform:SetScale(1, coldness > 2 and 1.2 or 0.8, 1)
			fx.Transform:SetPosition(target:GetPosition():Get())
		end
	end

	if math.random() < 0.05 then
		inst.components.musha_equipment:Gain_Exp(1)
	end
end

local function Transfer_Owner(inst)
	if not inst.components.blinkstaff then
		inst:AddComponent("blinkstaff")
		inst.components.blinkstaff:SetFX("sand_puff_large_front", "sand_puff_large_back")
		inst.components.blinkstaff.onblinkfn = function(staff, pos, caster)
			if caster.components.sanity ~= nil then
				caster.components.musha_spellpower:DoDelta(-5)
			end
		end
	end
end

local function Add_Transfer(inst)
	inst:RemoveEventCallback("level20", Add_Transfer)
	Transfer_Owner(inst)
end

local function OnEquip(inst, owner)
	--Updata(inst)

	inst.Light:SetRadius(.4)
	inst.Light:SetFalloff(.5)
	inst.Light:SetIntensity(.5)
	inst.Light:SetColour(240 / 255, 250 / 255, 255 / 255)
	owner.AnimState:OverrideSymbol("swap_object", "musha_sword_long_swap", "phoenixspear")
	owner.AnimState:Show("ARM_carry")
	owner.AnimState:Hide("ARM_normal")
end

local function Energy_Switch(inst)
	local owner = inst.components.inventoryitem.owner
	if not inst.EnergyOn then
		if inst.components.musha_equipment:IsBroken() then
			return
		end

		inst.EnergyOn = true
		inst.components.talker:Say(STRINGS.MUSHA_WEAPON_SWORD_LONG .. STRINGS.MUSHA_WEAPON_SWORD_POWER_ON)

		local owner = inst.components.inventoryitem.owner
		if inst.components.musha_equipment.currentlevel >= 20 then
			Transfer_Owner(inst)
		else
			inst:ListenForEvent("level20", Add_Transfer)
		end

		local change_fx = SpawnPrefab("splash")
		change_fx.entity:AddFollower():FollowSymbol(owner.GUID, "swap_object", 1, -150, 1)
		local change_fx = SpawnPrefab("small_puff")
		change_fx.entity:AddFollower():FollowSymbol(owner.GUID, "swap_object", 1, -150, 1)

		inst.components.musha_equipment:StartConsuming()
		inst.SoundEmitter:PlaySound("dontstarve/common/gem_shatter")
		if inst.components.musha_equipment.currentlevel < 10 then
			inst.Light:SetRadius(.3)
		elseif inst.components.musha_equipment.currentlevel < 20 then
			inst.components.equippable.walkspeedmult = 1.2
			inst.components.equippable.runspeedmult = 1.2
			inst.Light:SetRadius(.6)
		else
			inst.components.talker:Say(STRINGS.MUSHA_WEAPON_SWORD_FROST .. STRINGS.MUSHA_WEAPON_SWORD_POWER_3)
			inst.components.equippable.walkspeedmult = 1.25
			inst.components.equippable.runspeedmult = 1.25
			inst.Light:SetRadius(1)

			inst.Light:SetColour(255 / 255, 145 / 255, 100 / 255)
			inst.Light:SetFalloff(.5)
			inst.Light:SetIntensity(.5)
		end
	else
		inst.EnergyOn = false
		inst.components.musha_equipment:StopConsuming()
		inst.components.talker:Say("-" .. STRINGS.MUSHA_WEAPON_SWORD_FROST .. "\n" .. STRINGS.MUSHA_WEAPON_SWORD_POWER_OFF)
		if inst.components.musha_equipment.currentlevel >= 20 then
			inst:RemoveComponent("blinkstaff")
		else
			inst:RemoveEventCallback("level20", Add_Transfer)
		end
		inst.components.equippable.walkspeedmult = 1
		inst.components.equippable.runspeedmult = 1
		inst.SoundEmitter:PlaySound("dontstarve/common/fireOut")
		inst.components.weapon:SetRange(0)
		inst.Light:SetColour(240 / 255, 250 / 255, 255 / 255)
		inst.Light:SetRadius(.4)
		inst.Light:SetFalloff(.5)
		inst.Light:SetIntensity(.5)
	end
	inst.components.useableitem:StopUsingItem()
end

local function OnUnequip(inst, owner)
	if inst.EnergyOn then
		Energy_Switch(inst)
	end

	inst.Light:Enable(false)
	owner.AnimState:Hide("ARM_carry")
	owner.AnimState:Show("ARM_normal")
end

local function fn()
	local inst = CreateEntity()

	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	inst.entity:AddNetwork()

	inst.entity:AddLight()
	inst.Light:SetRadius(.4)
	inst.Light:SetFalloff(.5)
	inst.Light:SetIntensity(.5)
	inst.Light:SetColour(240 / 255, 250 / 255, 255 / 255)
	inst.Light:Enable(true)

	inst.AnimState:SetBank("musha_sword_long")
	inst.AnimState:SetBuild("musha_sword_long")
	inst.AnimState:PlayAnimation("idle")
	inst.AnimState:SetMultColour(1, 1, 1, 0.7)

	inst:AddTag("sharp")
	inst:AddTag("aquatic")
	inst:AddTag("musha_items")

	inst:AddComponent("talker")
	inst.components.talker.fontsize = 30
	inst.components.talker.font = TALKINGFONT
	inst.components.talker.colour = Vector3(0.9, 1, 0.75, 1)
	inst.components.talker.offset = Vector3(0, 150, 0)
	inst.components.talker.symbol = "swap_object"

	MakeInventoryPhysics(inst)
	MakeInventoryFloatable(inst, nil, nil)

	if TheNet:GetIsClient() then
		return inst
	end

	inst:AddComponent("useableitem")
	inst.components.useableitem:SetOnUseFn(Energy_Switch)

	inst.entity:SetPristine()

	inst:AddComponent("equippable")
	inst.components.equippable:SetOnEquip(OnEquip)
	inst.components.equippable:SetOnUnequip(OnUnequip)

	inst:AddComponent("musha_equipment")
	inst.components.musha_equipment:SetUpgradable(false)
	inst.components.musha_equipment:InitMushaEquip("weapon", 1050, 0.02)
	inst.components.musha_equipment:SetOnUpdataFn(Updata)
	inst.components.musha_equipment:SetOnBrokenFn(
		function()
			inst.components.inventoryitem.owner.components.talker:Say(STRINGS.MUSHA_WEAPON_BROKEN_TALK)
			if inst.EnergyOn then
				Energy_Switch(inst)
				inst.EnergyOn = true
			end
			local owner = inst.components.inventoryitem.owner
			owner.components.talker:Say(STRINGS.MUSHA_WEAPON_BROKEN_TALK)
			inst.components.talker:Say("-" .. STRINGS.MUSHA_WEAPON .. "\n" .. STRINGS.MUSHA_ITEM_DUR .. " (0)")
			inst:RemoveComponent("tool")
			Updata(inst)
		end
	)

	inst.components.weapon:SetRange(1.2)
	inst.components.inventoryitem.atlasname = "images/inventoryimages/musha_sword_long.xml"

	inst.BuffAttack = OnAttack
	inst.EnergyOn = false
	inst.type = "fireice"

	--[[ inst.Rebind = function(inst)
		if inst.EnergyOn then
			inst.EnergyOn = false
			Energy_Switch(inst)
		end
	end ]]
	return inst
end

return Prefab("musha_sword_long", fn, assets)
